package com.myapi.client.DAO;

import java.io.FileInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.Embedded;

import com.google.gwt.user.client.Timer;
import com.myapi.client.PlayerHandDAO;
import com.myapi.client.PotContributionInfo;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
		public class TableDAO implements Serializable {
	

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long ident;

	
	@NotPersistent
	private static TableDAO tableDAO;
	
	//@Persistent
	@NotPersistent 
	public static final int MAX_PLAYERS = 4;  
	 //@Persistent(serialized = "true")
	//  @Embedded
	@NotPersistent
	ArrayList<PlayerHandDAO> playerHands;
	@NotPersistent
	String id;  //unique ID for table, to allow for multiple tables at once (tournament style)
	@NotPersistent 
	int buttonPosition;  //the position (in Player list) of the dealer button. Small blind = + 1, Big blind = + 2
	//@Persistent 
	@Persistent
	ArrayList<TablePotDAO> pots;
	@NotPersistent
	DealerDAO dealer;
	@NotPersistent
	double currentBet;
	@NotPersistent
	int timeoutPlayer; // min=0 max=100;
	@Persistent
	public
	PlayerDAO currentPlayer;
	@NotPersistent
	int commCardCounter;
	//@Persistent
	@Persistent
	public 
	ArrayList<String> Forum;
	@NotPersistent 
	ArrayList<CardDAO> commCards;
	@NotPersistent
	public 
	boolean gameRunning;


	public TableDAO(){
		if(tableDAO==null){
			playerHands=new ArrayList<PlayerHandDAO>();
			pots=new ArrayList<TablePotDAO>();
			Forum=new ArrayList<String>();
		}

	}


	/**
	 * the active pot is always the last to be added
	 * @return TablePot
	 */
	public TablePotDAO getActivePot() {
		if(pots.size() > 0)
			return pots.get(pots.size() - 1);  //the active pot is always the last to be added
		else
			return null;            
	}
	
	public ArrayList<PlayerHandDAO> getPlayerHands() {
			if(playerHands.size()==0){
		ArrayList<PlayerHandDAO> playerHand=new ArrayList<PlayerHandDAO>();
		for(PotContributionInfo contribution :getPots().get(0).getContributions())
			playerHand.add(contribution.getPlayer().getPlayerHand());
		
		return playerHand;
		}
		else
			return this.playerHands;
	}



	public void setPlayerHands(ArrayList<PlayerHandDAO> playerHands) {
		this.playerHands = playerHands;
	}



	public String getId() {
		return id;
	}



	public void setId(String id) {
		this.id = id;
	}



	public int getButtonPosition() {
		return buttonPosition;
	}



	public void setButtonPosition(int buttonPosition) {
		this.buttonPosition = buttonPosition;
	}



	public ArrayList<TablePotDAO> getPots() {
		return pots;
	}



	public void setPots(ArrayList<TablePotDAO> pots) {
		this.pots = pots;
	}



	public DealerDAO getDealer() {
		return dealer;
	}



	public void setDealer(DealerDAO dealer) {
		this.dealer = dealer;
	}



	public double getCurrentBet() {
		return currentBet;
	}



	public void setCurrentBet(double currentBet) {
		this.currentBet = currentBet;
	}



	public ArrayList<String> getForum() {
		return Forum;
	}



	public void setForum(ArrayList<String> forum) {
		Forum = forum;
	}



	public static int getMAX_PLAYERS() {
		return MAX_PLAYERS;
	}






	public int getCommCardCounter() {
		return commCardCounter;
	}




	public void setCommCardCounter(int commCardCounter) {
		this.commCardCounter = commCardCounter;
	}




	public int getTimeoutPlayer() {
		return timeoutPlayer;
	}




	public void setTimeoutPlayer(int timeoutPlayer) {
		this.timeoutPlayer = timeoutPlayer;
	}




	public PlayerDAO getCurrentPlayer() {
		return currentPlayer;
	}




	public void setCurrentPlayer(PlayerDAO playerDAO) {
		this.currentPlayer = playerDAO;
	}


	public ArrayList<CardDAO> getCommCards() {
		return commCards;
	}


	public void setCommCards(ArrayList<CardDAO> commCards) {
		this.commCards = commCards;
	}


	public boolean isGameRunning() {
		return gameRunning;
	}


	public void setGameRunning(boolean gameRunning) {
		this.gameRunning = gameRunning;
	}

	
}
